<?php

namespace app\controller\admin;

use app\base\BaseEncryptController;
use app\model\content\ContentArticleModel;
use think\Request;

class ContentArticle extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new ContentArticleModel();
    }

    /**
     * 分页列表
     * @param string $type_id
     * @param string $keyword
     * @param $status
     * @param int $page
     * @param int $limit
     * @return \think\response\Json
     */
    public function getPage($type_id, $keyword = '', $status, $page = 1, $limit = 10)
    {
        $map = [];
        if (!empty($type_id)) {
            $map['type_id'] = $type_id;
        }
        if (!empty($status)) {
            $map['status'] = $status;
        }
        $data = $this->model->getPageData($map, $keyword, $page, $limit);
        return self::returnMsg(1, [
            'totalCount' => $data['total'],
            'currentPage' => $data['current'],
            'pageList' => $data['dataList']
        ]);
    }

    /**
     * 获取详情
     *
     * @param [type] $id
     * @return void
     */
    public function getInfo($id)
    {
        $data = $this->model->getInfo($id);
        if (empty($data)) {
            return self::returnMsg(1001);
        }
        return self::returnMsg(1, $data);
    }

    /**
     * 保存
     *
     * @param Request $request
     * @return void
     */
    public function onSave(Request $request)
    {
        $video_url = $request->param('video_url');
        $type = empty($video_url) ? 1 : 2;
        $formData = [
            'id' => $request->param('id'),
            'title' => $request->param('title'),
            'author' => $request->param('author'),
            'desc' => $request->param('desc'),
            'photo' => $request->param('photo'),
            'is_member' => $request->param('is_member'),
            'sort' => $request->param('sort'),
            'content' => $request->param('content'),
            'video_url' => $video_url,
            'type_id' => $request->param('type_id'),
            'type' => $type,
            'tag' => $request->param('tag'),
        ];
        if (empty($formData['id'])) {
            $validate = 'app\validate\ContentArticleValidate.add';
        } else {
            $validate = 'app\validate\ContentArticleValidate';
        }
        $result = $this->validate($formData, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData, false, false);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 修改状态
     *
     * @param Request $request
     * @return void
     */
    public function onStatus(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'status' => $request->param('status'),
        ];
        $validate = 'app\validate\ContentArticleValidate.upstatus';
        $result = $this->validate($formData, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData, false, false);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 删除
     *
     * @param [type] $id
     * @return void
     */
    public function onDelete($id)
    {
        $flag = $this->model::destroy(['id' => $id]);
        if (false !== $flag) {
            $this->model->delRedis($id);
            $this->model->redis->zRem($this->model->redis_key . ':rank', $id);
            $this->model->addToRecycle($id, 'ContentArticleModel', '图文动态管理', $this->currentUser['user_id'], $this->currentUser['user_type'], 'content');
            return self::returnMsg(1);
        } else {
            return self::returnMsg(1002);
        }
    }
}
